package com.jumptap.adtag.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import com.jumptap.adtag.events.EventType;
import com.jumptap.adtag.events.JtEvent;
import com.jumptap.adtag.media.VideoCacheItem;
import java.util.ArrayList;
import java.util.List;

public class DBManager
{
  private static final String ADID_COLUMN = "adid";
  private static final String CREATE_PENDING_EVENTS_QUERY = "CREATE TABLE IF NOT EXISTS pending_events(id INTEGER PRIMARY KEY, eventType TEXT, date TEXT, url TEXT);";
  private static final String CREATE_VIDEO_CACHE_QUERY = "CREATE TABLE IF NOT EXISTS video_cache(id INTEGER PRIMARY KEY, adid INTEGER,date TEXT);";
  private static final String DATABASE_NAME = "jt_ad_view.db";
  private static final int DATABASE_VERSION = 5;
  private static final String DATE_COLUMN = "date";
  private static final String DROP_QUERY = "DROP TABLE IF EXISTS pending_events; DROP TABLE IF EXISTS video_cache;";
  private static final String EVENT_TYPE_COLUMN = "eventType";
  private static final String ID_COLUMN = "id";
  private static final String INSERT_EVENT_QUERY = "insert into pending_events(eventType,date,url) values (?,?,?)";
  private static final String INSERT_VIDEO_CACHE_QUERY = "insert into video_cache(adid,date) values (?,?)";
  private static final String PENDING_EVENTS_TABLE_NAME = "pending_events";
  private static final String URL_COLUMN = "url";
  private static final String VIDEO_CACHE_TABLE_NAME = "video_cache";
  private static DBManager dbManagerInstance;
  private Context context;
  private SQLiteDatabase db;
  private SQLiteStatement insertStmt;

  private DBManager(Context paramContext)
  {
    this.context = paramContext;
    openDB();
  }

  public static DBManager getInstance(Context paramContext)
  {
    if (dbManagerInstance == null)
      dbManagerInstance = new DBManager(paramContext);
    return dbManagerInstance;
  }

  private void openDB()
  {
    this.db = new JtSQLiteOpenHelper(this.context).getWritableDatabase();
  }

  public void close()
  {
    this.db.close();
  }

  public void deleteAllPendingEvents()
  {
    synchronized (dbManagerInstance)
    {
      try
      {
        if (!this.db.isOpen())
          openDB();
        this.db.delete("pending_events", null, null);
        return;
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
          Log.d("JtAd", "Failed to delete all pending events table", localIllegalStateException);
      }
    }
  }

  public void deleteAllVideoCacheItems()
  {
    synchronized (dbManagerInstance)
    {
      try
      {
        if (!this.db.isOpen())
          openDB();
        this.db.delete("video_cache", null, null);
        return;
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
          Log.d("JtAd", "Failed to delete all video cache items table", localIllegalStateException);
      }
    }
  }

  public void deleteEventById(int paramInt)
  {
    synchronized (dbManagerInstance)
    {
      try
      {
        if (!this.db.isOpen())
          openDB();
        SQLiteDatabase localSQLiteDatabase = this.db;
        String[] arrayOfString = new String[1];
        arrayOfString[0] = Integer.toString(paramInt);
        localSQLiteDatabase.delete("pending_events", "id=?", arrayOfString);
        return;
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
          Log.d("JtAd", "Failed to delete event by id=" + paramInt, localIllegalStateException);
      }
    }
  }

  public void deleteVideoCacheItemByAdId(String paramString)
  {
    synchronized (dbManagerInstance)
    {
      try
      {
        if (!this.db.isOpen())
          openDB();
        this.db.delete("video_cache", "adid=?", new String[] { paramString });
        return;
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
          Log.d("JtAd", "Failed to delete video chache item by adid=" + paramString, localIllegalStateException);
      }
    }
  }

  public void deleteVideoCacheItemById(int paramInt)
  {
    synchronized (dbManagerInstance)
    {
      try
      {
        if (!this.db.isOpen())
          openDB();
        SQLiteDatabase localSQLiteDatabase = this.db;
        String[] arrayOfString = new String[1];
        arrayOfString[0] = Integer.toString(paramInt);
        localSQLiteDatabase.delete("video_cache", "id=?", arrayOfString);
        return;
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
          Log.d("JtAd", "Failed to delete video chache item by id=" + paramInt, localIllegalStateException);
      }
    }
  }

  public long insertEvent(JtEvent paramJtEvent)
  {
    synchronized (dbManagerInstance)
    {
      if (!this.db.isOpen())
        openDB();
      this.insertStmt = this.db.compileStatement("insert into pending_events(eventType,date,url) values (?,?,?)");
      this.insertStmt.bindString(1, paramJtEvent.getEventType().name());
      this.insertStmt.bindString(2, paramJtEvent.getDate());
      this.insertStmt.bindString(3, paramJtEvent.getUrl());
      long l = this.insertStmt.executeInsert();
      return l;
    }
  }

  public long insertVideoCacheItem(VideoCacheItem paramVideoCacheItem)
  {
    synchronized (dbManagerInstance)
    {
      if (!this.db.isOpen())
        openDB();
      this.insertStmt = this.db.compileStatement("insert into video_cache(adid,date) values (?,?)");
      this.insertStmt.bindString(1, paramVideoCacheItem.getAdID());
      this.insertStmt.bindString(2, paramVideoCacheItem.getDate());
      long l = this.insertStmt.executeInsert();
      return l;
    }
  }

  // ERROR //
  public List<JtEvent> selectAllEvents()
  {
    // Byte code:
    //   0: getstatic 69	com/jumptap/adtag/db/DBManager:dbManagerInstance	Lcom/jumptap/adtag/db/DBManager;
    //   3: astore_1
    //   4: aload_1
    //   5: monitorenter
    //   6: aconst_null
    //   7: astore_2
    //   8: aconst_null
    //   9: astore_3
    //   10: new 192	java/util/ArrayList
    //   13: dup
    //   14: invokespecial 193	java/util/ArrayList:<init>	()V
    //   17: astore 4
    //   19: aload_0
    //   20: getfield 80	com/jumptap/adtag/db/DBManager:db	Landroid/database/sqlite/SQLiteDatabase;
    //   23: invokevirtual 92	android/database/sqlite/SQLiteDatabase:isOpen	()Z
    //   26: istore 8
    //   28: aconst_null
    //   29: astore_3
    //   30: aconst_null
    //   31: astore_2
    //   32: iload 8
    //   34: ifne +7 -> 41
    //   37: aload_0
    //   38: invokespecial 65	com/jumptap/adtag/db/DBManager:openDB	()V
    //   41: aload_0
    //   42: getfield 80	com/jumptap/adtag/db/DBManager:db	Landroid/database/sqlite/SQLiteDatabase;
    //   45: ldc 41
    //   47: iconst_4
    //   48: anewarray 113	java/lang/String
    //   51: dup
    //   52: iconst_0
    //   53: ldc 32
    //   55: aastore
    //   56: dup
    //   57: iconst_1
    //   58: ldc 29
    //   60: aastore
    //   61: dup
    //   62: iconst_2
    //   63: ldc 23
    //   65: aastore
    //   66: dup
    //   67: iconst_3
    //   68: ldc 44
    //   70: aastore
    //   71: aconst_null
    //   72: aconst_null
    //   73: aconst_null
    //   74: aconst_null
    //   75: ldc 195
    //   77: invokevirtual 199	android/database/sqlite/SQLiteDatabase:query	(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;
    //   80: astore_2
    //   81: aload_2
    //   82: invokeinterface 204 1 0
    //   87: istore 9
    //   89: aconst_null
    //   90: astore_3
    //   91: iload 9
    //   93: ifeq +90 -> 183
    //   96: aload_3
    //   97: astore 11
    //   99: aload_2
    //   100: iconst_0
    //   101: invokeinterface 208 2 0
    //   106: istore 12
    //   108: aload_2
    //   109: iconst_1
    //   110: invokeinterface 211 2 0
    //   115: ifnull +147 -> 262
    //   118: aload_2
    //   119: iconst_1
    //   120: invokeinterface 211 2 0
    //   125: invokestatic 215	com/jumptap/adtag/events/EventType:valueOf	(Ljava/lang/String;)Lcom/jumptap/adtag/events/EventType;
    //   128: astore 15
    //   130: aload_2
    //   131: iconst_2
    //   132: invokeinterface 211 2 0
    //   137: astore 16
    //   139: aload 4
    //   141: new 155	com/jumptap/adtag/events/JtEvent
    //   144: dup
    //   145: iload 12
    //   147: aload_2
    //   148: iconst_3
    //   149: invokeinterface 211 2 0
    //   154: aload 15
    //   156: aload 16
    //   158: invokespecial 218	com/jumptap/adtag/events/JtEvent:<init>	(ILjava/lang/String;Lcom/jumptap/adtag/events/EventType;Ljava/lang/String;)V
    //   161: invokeinterface 224 2 0
    //   166: pop
    //   167: aload 11
    //   169: astore_3
    //   170: aload_2
    //   171: invokeinterface 227 1 0
    //   176: istore 14
    //   178: iload 14
    //   180: ifne -84 -> 96
    //   183: aload_2
    //   184: ifnull +18 -> 202
    //   187: aload_2
    //   188: invokeinterface 230 1 0
    //   193: ifne +9 -> 202
    //   196: aload_2
    //   197: invokeinterface 231 1 0
    //   202: aload_3
    //   203: ifnull +165 -> 368
    //   206: aload_3
    //   207: invokeinterface 235 1 0
    //   212: bipush 10
    //   214: if_icmple +154 -> 368
    //   217: aload_3
    //   218: invokeinterface 239 1 0
    //   223: astore 10
    //   225: aload 10
    //   227: invokeinterface 244 1 0
    //   232: ifeq +136 -> 368
    //   235: aload_0
    //   236: aload 10
    //   238: invokeinterface 248 1 0
    //   243: checkcast 115	java/lang/Integer
    //   246: invokevirtual 251	java/lang/Integer:intValue	()I
    //   249: invokevirtual 253	com/jumptap/adtag/db/DBManager:deleteEventById	(I)V
    //   252: goto -27 -> 225
    //   255: astore 6
    //   257: aload_1
    //   258: monitorexit
    //   259: aload 6
    //   261: athrow
    //   262: aload 11
    //   264: ifnonnull +120 -> 384
    //   267: new 192	java/util/ArrayList
    //   270: dup
    //   271: invokespecial 193	java/util/ArrayList:<init>	()V
    //   274: astore_3
    //   275: aload_3
    //   276: new 115	java/lang/Integer
    //   279: dup
    //   280: iload 12
    //   282: invokespecial 255	java/lang/Integer:<init>	(I)V
    //   285: invokeinterface 224 2 0
    //   290: pop
    //   291: goto -121 -> 170
    //   294: astore 5
    //   296: aload_2
    //   297: ifnull +18 -> 315
    //   300: aload_2
    //   301: invokeinterface 230 1 0
    //   306: ifne +9 -> 315
    //   309: aload_2
    //   310: invokeinterface 231 1 0
    //   315: aload_3
    //   316: ifnull +57 -> 373
    //   319: aload_3
    //   320: invokeinterface 235 1 0
    //   325: bipush 10
    //   327: if_icmple +46 -> 373
    //   330: aload_3
    //   331: invokeinterface 239 1 0
    //   336: astore 7
    //   338: aload 7
    //   340: invokeinterface 244 1 0
    //   345: ifeq +28 -> 373
    //   348: aload_0
    //   349: aload 7
    //   351: invokeinterface 248 1 0
    //   356: checkcast 115	java/lang/Integer
    //   359: invokevirtual 251	java/lang/Integer:intValue	()I
    //   362: invokevirtual 253	com/jumptap/adtag/db/DBManager:deleteEventById	(I)V
    //   365: goto -27 -> 338
    //   368: aload_1
    //   369: monitorexit
    //   370: aload 4
    //   372: areturn
    //   373: aload 5
    //   375: athrow
    //   376: astore 5
    //   378: aload 11
    //   380: astore_3
    //   381: goto -85 -> 296
    //   384: aload 11
    //   386: astore_3
    //   387: goto -112 -> 275
    //
    // Exception table:
    //   from	to	target	type
    //   187	202	255	finally
    //   206	225	255	finally
    //   225	252	255	finally
    //   257	259	255	finally
    //   300	315	255	finally
    //   319	338	255	finally
    //   338	365	255	finally
    //   368	370	255	finally
    //   373	376	255	finally
    //   10	28	294	finally
    //   37	41	294	finally
    //   41	89	294	finally
    //   170	178	294	finally
    //   275	291	294	finally
    //   99	167	376	finally
    //   267	275	376	finally
  }

  public List<VideoCacheItem> selectAllVideoCacheItems()
  {
    synchronized (dbManagerInstance)
    {
      ArrayList localArrayList = new ArrayList();
      if (!this.db.isOpen())
        openDB();
      Cursor localCursor = this.db.query("video_cache", new String[] { "id", "adid", "date" }, null, null, null, null, "id desc");
      if (localCursor.moveToFirst())
        do
          localArrayList.add(new VideoCacheItem(localCursor.getInt(0), localCursor.getString(1), localCursor.getString(2)));
        while (localCursor.moveToNext());
      if ((localCursor != null) && (!localCursor.isClosed()))
        localCursor.close();
      return localArrayList;
    }
  }

  private static class JtSQLiteOpenHelper extends SQLiteOpenHelper
  {
    JtSQLiteOpenHelper(Context paramContext)
    {
      super("jt_ad_view.db", null, 5);
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      Log.w("JtAd", "Creating database.");
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS pending_events(id INTEGER PRIMARY KEY, eventType TEXT, date TEXT, url TEXT);");
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS video_cache(id INTEGER PRIMARY KEY, adid INTEGER,date TEXT);");
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      Log.w("JtAd", "Upgrading database, this will drop tables and recreate.");
      paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS pending_events; DROP TABLE IF EXISTS video_cache;");
      onCreate(paramSQLiteDatabase);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.jumptap.adtag.db.DBManager
 * JD-Core Version:    0.6.2
 */